<?php

$oper = $_GET['oper'];

if (!isset($oper))
{
	die("Unknown command");
}

require('../dbconnect.inc.php');

switch ($oper)
{

	case 'info':

			if (!isset($_GET['cid']))
			{
				$response->error = 'Wrong params !';
				echo json_encode($response);
				return;
			}
			
			$rs = $dbh->query('SELECT network_map.c_id, network_map.g_id,network_map.location,'.
							'network_map.enc_key, network_map.prog_update_time, network_map.prog_load_time,'.
							'network_map.next_enc_key, network_map.prev_enc_key, network_map.prog_update_status,'.
							'c_status.job_mode,c_status.flash_status,c_status.program as prog_status,'.
							'c_status.phase,c_status.fault,c_status.fault_red,c_status.fault_yellow,'.
							'c_status.fault_green,c_status.no_program,c_status.phase_time,c_status.connect_time,'.
							'c_status.cont_scan_time,c_status.cont_local_time'.
							' FROM network_map LEFT OUTER JOIN c_status ON ' .
							'network_map.c_id = c_status.c_id WHERE network_map.c_id=' . $_GET['cid']);

			if ($rec = $rs->fetchAll(PDO::FETCH_ASSOC))	
			{
				foreach($rec[0] as $key => $val)
				{
					$response->$key = $val;
				}
			}
			else
			{
				$response->error = 'Controller not found';
			}

			echo json_encode($response);

		break;

case 'all':

			if (isset($_GET['gid']))
			{
				if (intval($_GET['gid']) == -1)
				{

					if (!isset($_GET['uid']))
					{
						$response->error = 'Wrong params !';
						echo json_encode($response);
						return;
					}

					$query = 'SELECT network_map.c_id, network_map.g_id, network_map.location,'.
							'network_map.enc_key, network_map.prog_update_time, network_map.prog_load_time,'.
							'network_map.next_enc_key, network_map.prev_enc_key, network_map.prog_update_status,'.
							'c_status.job_mode,c_status.flash_status,c_status.program as prog_status,'.
							'c_status.phase,c_status.fault,c_status.fault_red,c_status.fault_yellow,'.
							'c_status.fault_green,c_status.no_program,c_status.phase_time,c_status.connect_time,'.
							'c_status.cont_scan_time,c_status.cont_local_time '.
							'FROM network_map LEFT OUTER JOIN c_status ON network_map.c_id = c_status.c_id, users_groups '.
							'WHERE users_groups.g_id = network_map.g_id AND users_groups.u_id=' . intval($_GET['uid']) .
							' ORDER BY network_map.c_id';

				}
				else
				{
					$query = 'SELECT network_map.c_id, network_map.g_id,network_map.location,'.
							'network_map.enc_key, network_map.prog_update_time, network_map.prog_load_time,'.
							'network_map.next_enc_key, network_map.prev_enc_key, network_map.prog_update_status,'.
							'c_status.job_mode,c_status.flash_status,c_status.program as prog_status,'.
							'c_status.phase,c_status.fault,c_status.fault_red,c_status.fault_yellow,'.
							'c_status.fault_green,c_status.no_program,c_status.phase_time,c_status.connect_time,'.
							'c_status.cont_scan_time,c_status.cont_local_time'.
							' FROM network_map LEFT OUTER JOIN c_status ON ' .
							'network_map.c_id = c_status.c_id WHERE network_map.g_id='.intval($_GET['gid']).
							' ORDER BY network_map.c_id';
				}
			}
			else
			{
				$query = 'SELECT network_map.c_id, network_map.g_id,network_map.location,'.
							'network_map.enc_key, network_map.prog_update_time, network_map.prog_load_time,'.
							'network_map.next_enc_key, network_map.prev_enc_key, network_map.prog_update_status,'.
							'c_status.job_mode,c_status.flash_status,c_status.program as prog_status,'.
							'c_status.phase,c_status.fault,c_status.fault_red,c_status.fault_yellow,'.
							'c_status.fault_green,c_status.no_program,c_status.phase_time,c_status.connect_time,'.
							'c_status.cont_scan_time,c_status.cont_local_time'.
							' FROM network_map LEFT OUTER JOIN c_status ON ' .
							'network_map.c_id = c_status.c_id ORDER BY network_map.c_id';
			}
			
			$rs = $dbh->query($query);

			if ($rec = $rs->fetchAll(PDO::FETCH_ASSOC))	
			{
				foreach($rec as $rowkey => $rowval)
				{
					foreach($rowval as $key => $val)
					{
						$response[$rowkey]->$key = $val;
					}
				}
			}
			else
			{
				$response->error = 'Controller not found';
			}

			echo json_encode($response);

		break;

	case 'cmd':
		
			if (!isset($_GET['cid']))
			{
				$response->error = 'Wrong params !';
				echo json_encode($response);
				return;
			}

			$cnt = intval($_GET['cid']);
			$param = intval($_GET['param']);

			switch ($_GET['cmd'])
			{
				case 'pload':
					$cmd = 17;	
				break;

				case 'phase':
					$cmd = 23;	
				break;

				case 'sprog':
					$cmd = 18;	
				break;

				case 'stime':
					$cmd = 16;	
				break;
			}

			$sth = $dbh->prepare('insert into cmd_queue (uid, c_id, cmd, param) values (UUID(),:cid,:cmd, :param)');

			$sth->bindParam(':cid', $cnt, PDO::PARAM_INT);
			$sth->bindParam(':cmd', $cmd, PDO::PARAM_INT);
			$sth->bindParam(':param', $param, PDO::PARAM_INT);

			if (!$sth->execute())
			{
				$response->error = 'SQL Error !';
				echo json_encode($response);
				return;
			}

			$response->status = 'Command send succeful !';
			echo json_encode($response);
			return;


	break;

}

?>